package GUI;

import javax.swing.*;

import Themes.Theme;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import Data.*;

public class MainFrame extends JFrame implements ActionListener {
	ResourceBundle lang;
	Options options;
	Log log;
	File file;
	Classes classes;
	Lines lines;
	
	private boolean unSaved = false;
	
	DesktopPane desktopPane;
	ClassFrame lastActivedClassFrame;
	
	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenuItem newMenuItem;
	private JMenuItem openMenuItem;
	private JMenuItem saveMenuItem;
	private JMenuItem saveAsMenuItem;
	private JMenuItem exitMenuItem;
	private JMenu editMenu;
	private JMenuItem undoMenuItem;
	private JMenuItem redoMenuItem;
	private JMenuItem duplicateMenuItem;
	private JMenuItem deleteMenuItem;
	private JMenuItem preferencesMenuItem;
	private JMenu projectMenu;
	private JMenuItem addClassMenuItem;
	private JMenuItem hasAMenuItem;
	private JMenuItem isAMenuItem;
	private JMenuItem unLinkMenuItem;
	private JMenuItem generateMenuItem;
	private JMenuItem generateAllMenuItem;
	private JMenu helpMenu;
	private JMenuItem contentsMenuItem;
	private JMenuItem aboutMenuItem;
	
	private JToolBar toolBar;
	private JButton newToolBarButton;
	private JButton openToolBarButton;
	private JButton saveToolBarButton;
	private JButton undoToolBarButton;
	private JButton redoToolBarButton;
	private JButton copyToolBarButton;
	private JButton duplicateToolBarButton;
	private JButton hasAToolBarButton;
	private JButton generateToolBarButton;
	private JButton generateAllToolBarButton;
	//private JButton
	
	
	
	public MainFrame() {
		options = Options.getInstance();
		log = Log.getInstance();
		lang = Options.getLanguage();
		classes = Classes.getInstance();
		lines = Lines.getInstance();
		
		desktopPane = new DesktopPane();

		add(desktopPane);

		//try {
			super.setTitle(lang.getString("MainFrame.text"));
			createMenuBar();
			createToolBar();
		//} catch (MissingResourceException missingResourceException) {
			
		//}
	}
	
	private void createMenuBar() {
		ResourceBundle lang = Options.getLanguage();
		Theme theme = Theme.getInstance();
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu();
		newMenuItem = new JMenuItem();
		openMenuItem = new JMenuItem();
		saveMenuItem = new JMenuItem();
		saveAsMenuItem = new JMenuItem();
		exitMenuItem = new JMenuItem();
		editMenu = new JMenu();
		undoMenuItem = new JMenuItem();
		redoMenuItem = new JMenuItem();
		duplicateMenuItem = new JMenuItem();
		deleteMenuItem = new JMenuItem();
		preferencesMenuItem = new JMenuItem();
		projectMenu = new JMenu();
		hasAMenuItem = new JMenuItem();
		isAMenuItem = new JMenuItem();
		unLinkMenuItem = new JMenuItem();
		generateMenuItem = new JMenuItem();
		generateAllMenuItem = new JMenuItem();
		addClassMenuItem = new JMenuItem();
		helpMenu = new JMenu();
		contentsMenuItem = new JMenuItem();
		aboutMenuItem = new JMenuItem();		
		
		fileMenu.setText(lang.getString("MainFrame.fileMenu.text"));
		menuBar.add(fileMenu);
		newMenuItem.setText(lang.getString("MainFrame.newMenuItem.text"));
		newMenuItem.setIcon(theme.getImage("newIcon"));
		newMenuItem.addActionListener(this);
		fileMenu.add(newMenuItem);
		openMenuItem.setText(lang.getString("MainFrame.openMenuItem.text"));
		openMenuItem.setIcon(theme.getImage("openIcon"));
		openMenuItem.addActionListener(this);
		fileMenu.add(openMenuItem);
		fileMenu.addSeparator();
		saveMenuItem.setText(lang.getString("MainFrame.saveMenuItem.text"));
		saveMenuItem.setIcon(theme.getImage("saveIcon"));
		saveMenuItem.addActionListener(this);
		fileMenu.add(saveMenuItem);
		saveAsMenuItem.setText(lang.getString("MainFrame.saveAsMenuItem.text"));
		saveAsMenuItem.addActionListener(this);
		fileMenu.add(saveAsMenuItem);
		fileMenu.addSeparator();
		exitMenuItem.setText(lang.getString("MainFrame.exitMenuItem.text"));
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);
		
		editMenu.setText(lang.getString("MainFrame.editMenu.text"));
		menuBar.add(editMenu);
		undoMenuItem.setText(lang.getString("MainFrame.undoMenuItem.text"));
		undoMenuItem.addActionListener(this);
		editMenu.add(undoMenuItem);
		redoMenuItem.setText(lang.getString("MainFrame.redoMenuItem.text"));
		redoMenuItem.addActionListener(this);
		editMenu.add(redoMenuItem);
		editMenu.addSeparator();
		duplicateMenuItem.setText(lang.getString("MainFrame.duplicateMenuItem.text"));
		duplicateMenuItem.addActionListener(this);
		editMenu.add(duplicateMenuItem);
		deleteMenuItem.setText(lang.getString("MainFrame.deleteMenuItem.text"));
		deleteMenuItem.addActionListener(this);
		editMenu.add(deleteMenuItem);
		editMenu.addSeparator();
		preferencesMenuItem.setText(lang.getString("MainFrame.preferencesMenuItem.text"));
		preferencesMenuItem.addActionListener(this);
		editMenu.add(preferencesMenuItem);
		
		projectMenu.setText(lang.getString("MainFrame.projectMenu.text"));
		menuBar.add(projectMenu);
		addClassMenuItem.setText(lang.getString("MainFrame.addClassMenuItem.text"));
		addClassMenuItem.addActionListener(this);
		projectMenu.add(addClassMenuItem);
		projectMenu.addSeparator();
		hasAMenuItem.setText(lang.getString("MainFrame.hasAMenuItem.text"));
		hasAMenuItem.addActionListener(this);
		projectMenu.add(hasAMenuItem);
		isAMenuItem.setText(lang.getString("MainFrame.isAMenuItem.text"));
		isAMenuItem.addActionListener(this);
		projectMenu.add(isAMenuItem);
		unLinkMenuItem.setText(lang.getString("MainFrame.unLinkMenuItem.text"));
		unLinkMenuItem.addActionListener(this);
		projectMenu.add(unLinkMenuItem);
		projectMenu.addSeparator();
		generateMenuItem.setText(lang.getString("MainFrame.generateMenuItem.text"));
		generateMenuItem.addActionListener(this);
		projectMenu.add(generateMenuItem);
		generateAllMenuItem.setText(lang.getString("MainFrame.generateAllMenuItem.text"));
		generateAllMenuItem.addActionListener(this);
		projectMenu.add(generateAllMenuItem);
		
		helpMenu.setText(lang.getString("MainFrame.helpMenu.text"));
		menuBar.add(helpMenu);
		contentsMenuItem.setText(lang.getString("MainFrame.contentsMenuItem"));
		contentsMenuItem.addActionListener(this);
		helpMenu.add(contentsMenuItem);
		aboutMenuItem.setText(lang.getString("MainFrame.aboutMenuItem"));
		aboutMenuItem.addActionListener(this);
		helpMenu.add(aboutMenuItem);
		
		setJMenuBar(menuBar);
	}
	
	private void createToolBar() {
		ResourceBundle lang = Options.getLanguage();
		Theme theme = Theme.getInstance();
		
		toolBar = new JToolBar("Tool bar");
		newToolBarButton = new JButton();
		openToolBarButton = new JButton();
		saveToolBarButton = new JButton();
		undoToolBarButton = new JButton();
		redoToolBarButton = new JButton();
		copyToolBarButton = new JButton();
		duplicateToolBarButton = new JButton();
		hasAToolBarButton = new JButton();
		generateToolBarButton = new JButton();
		generateAllToolBarButton = new JButton();
		
		
		newToolBarButton.setBorder(null);
		openToolBarButton.setBorder(null);
		saveToolBarButton.setBorder(null);
		undoToolBarButton.setBorder(null);
		redoToolBarButton.setBorder(null);
		copyToolBarButton.setBorder(null);
		duplicateToolBarButton.setBorder(null);
		generateToolBarButton.setBorder(null);
		generateAllToolBarButton.setBorder(null);
		newToolBarButton.setBorder(null);
		
		newToolBarButton.setIcon(theme.getImage("newIcon"));
		openToolBarButton.setIcon(theme.getImage("openIcon"));
		saveToolBarButton.setIcon(theme.getImage("saveIcon"));
		undoToolBarButton.setIcon(theme.getImage("undoIcon"));
		redoToolBarButton.setIcon(theme.getImage("redoIcon"));
		copyToolBarButton.setIcon(theme.getImage("copyIcon"));
		duplicateToolBarButton.setIcon(theme.getImage("duplicateIcon"));
		hasAToolBarButton.setIcon(theme.getImage("duplicateIcon"));
		generateToolBarButton.setIcon(theme.getImage("generateIcon"));
		generateAllToolBarButton.setIcon(theme.getImage("generateAllIcon"));
		
		newToolBarButton.setToolTipText(lang.getString("MainFrame.newToolBarButton.toolTipText"));
		openToolBarButton.setToolTipText(lang.getString("MainFrame.openToolBarButton.toolTipText"));
		saveToolBarButton.setToolTipText(lang.getString("MainFrame.saveToolBarButton.toolTipText"));
		undoToolBarButton.setToolTipText(lang.getString("MainFrame.undoToolBarButton.toolTipText"));
		redoToolBarButton.setToolTipText(lang.getString("MainFrame.redoToolBarButton.toolTipText"));
		copyToolBarButton.setToolTipText(lang.getString("MainFrame.copyToolBarButton.toolTipText"));
		hasAToolBarButton.setToolTipText(lang.getString("MainFrame.copyToolBarButton.toolTipText"));
		duplicateToolBarButton.setToolTipText(lang.getString("MainFrame.duplicateToolBarButton.toolTipText"));
		generateToolBarButton.setToolTipText(lang.getString("MainFrame.generateToolBarButton.toolTipText"));
		generateAllToolBarButton.setToolTipText(lang.getString("MainFrame.generateAllToolBarButton.toolTipText"));		
		
		toolBar.add(newToolBarButton);
		toolBar.add(openToolBarButton);
		toolBar.add(saveToolBarButton);
		toolBar.addSeparator();
		toolBar.add(undoToolBarButton);
		toolBar.add(redoToolBarButton);
		toolBar.add(copyToolBarButton);
		toolBar.add(duplicateToolBarButton);
		toolBar.add(hasAToolBarButton);
		toolBar.add(generateToolBarButton);
		toolBar.add(generateAllToolBarButton);
		
		add(toolBar, BorderLayout.PAGE_START);
	}
	
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == newMenuItem) {
			newMenuItem_Clicked();
		}
		if (event.getSource() == openMenuItem) {
			openMenuItem_Clicked();
		}
		if (event.getSource() == saveMenuItem) {
			saveMenuItem_Clicked();
		}
		if (event.getSource() == saveAsMenuItem) {
			saveAsMenuItem_Clicked();
		}
		if (event.getSource() == exitMenuItem) {
			exitMenuItem_Clicked();
		}
		if (event.getSource() == undoMenuItem) {
			unreMenuItem_Clicked(1);
		}
		if (event.getSource() == redoMenuItem) {
			unreMenuItem_Clicked(-1);
		}
		if (event.getSource() == duplicateMenuItem) {
			duplicateMenuItem_Clicked();
		}
		if (event.getSource() == deleteMenuItem) {
			deleteMenuItem_Clicked();
		}
		if (event.getSource() == preferencesMenuItem) {
			preferencesMenuItem_Clicked();
		}
		if (event.getSource() == addClassMenuItem) {
			addClassMenuItem_Clicked();
		}
		if (event.getSource() == hasAMenuItem) {
			hasAMenuItem_Clicked();
		}
		if (event.getSource() == isAMenuItem) {
			isAMenuItem_Clicked();
		}
		if (event.getSource() == unLinkMenuItem) {
			unLinkMenuItem_Clicked();
		}
		if (event.getSource() == generateMenuItem) {
			generateMenuItem_Clicked();
		}
		if (event.getSource() == generateAllMenuItem) {
			generateAllMenuItem_Clicked();
		}
		if (event.getSource() == contentsMenuItem) {
			contentsMenuItem_Clicked();
		}
		if (event.getSource() == aboutMenuItem) {
			aboutMenuItem_Clicked();
		}
		
	}
	
	public void newMenuItem_Clicked() {
		if (unSaved) {
			int returnVal = Message.showConfirm(lang.getString("MainFrame.message.unSavedConfirm"));
			if (returnVal == JOptionPane.YES_OPTION) {
				saveMenuItem_Clicked();
			}
		} else {
			log.reset();
			desktopPane.removeAll();
			desktopPane.updateUI();
			unSaved = false;
			file = null;
		}
	}
	
	public void openMenuItem_Clicked() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new JCGFilter());
		
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			try {
				//XML_IO.open(file);
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.openFileError"));
			}
		}
	}
	
	public void saveMenuItem_Clicked() {
		if (file == null) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileFilter(new JCGFilter());
			int returnVal = fileChooser.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = fileChooser.getSelectedFile();
			} else return;
		}
		try {
			//XML_IO.save(file);
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.saveFileError"));
		}
		
	}
	
	public void saveAsMenuItem_Clicked() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new JCGFilter());
		
		int returnVal = fileChooser.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			try {
				//XML_IO.save(file);
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.saveFileError"));
			}
		}
	}
	
	public void exitMenuItem_Clicked() {
		
	}
	
	public void unreMenuItem_Clicked(int type) {
		ClassWithShape cwt;
		if (type == 1 && log.canGetPrevious()) {
			cwt = log.getPrevious();
		} else if (type == -1 && log.canGetNext()) {
			cwt = log.getNext();
		} else return;
		if (cwt.getOldClassFrame() == null) {
			cwt.getCurrentClassFrame().setVisible(false);
			desktopPane.updateUI();
		//} else if (cwt.getCurrentClassFrame() == null) {
			//ClassFrame cf = new ClassFrame(cwt.getOldClassFrame());
			//desktopPane.add(cf);
			//classes.addClass(cwt.getOldClassFrame().classData);
		} else {
			classes.removeClass(cwt.getCurrentClassFrame().classData);
			cwt.getCurrentClassFrame().copy(cwt.getOldClassFrame());
			cwt.getCurrentClassFrame().setVisible(true);
			classes.addClass(cwt.getCurrentClassFrame().classData);
		}
	}
	
	public void duplicateMenuItem_Clicked() {
		ClassFrame t = new ClassFrame((ClassFrame)this.desktopPane.getSelectedFrame());
		classes.addClass(t.classData);
		desktopPane.add(t);
		log.push(new ClassWithShape(null, null, t));
		log.push(new ClassWithShape(t.classData, t));
		desktopPane.updateUI();
	}
	
	public void deleteMenuItem_Clicked() {
		ClassFrame activedClassFrame = (ClassFrame)this.desktopPane.getSelectedFrame();
		if (this.desktopPane.getSelectedFrame() != null) {
			log.push(new ClassWithShape(activedClassFrame.classData, activedClassFrame, null));
			classes.removeClass(activedClassFrame.classData);
			//desktopPane.remove(activedClassFrame);
			this.desktopPane.getSelectedFrame().setVisible(false);
			desktopPane.updateUI();
		}
	}
	
	public void preferencesMenuItem_Clicked() {
		
	}
	
	public void addClassMenuItem_Clicked() {
		ClassFrame t = new ClassFrame(this);
		desktopPane.add(t);
		classes.addClass(t.classData);
		log.push(new ClassWithShape(null, null, t));
		log.push(new ClassWithShape(t.classData, t));
	}	
	
	public void hasAMenuItem_Clicked() {
		try {
			lines.add(new Line(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame(), Line.HAS_A));
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.hasAError"));
		}
	}
	
	public void isAMenuItem_Clicked() {
		try {
			lines.add(new Line(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame(), Line.IS_A));
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.isAError"));
		}
	}
	
	public void unLinkMenuItem_Clicked() {
		try {
			lines.remove(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame());
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.unLinkError"));
			System.out.print(e.getMessage());
		}
	}
	
	public void generateMenuItem_Clicked() {

	}
	
	public void generateAllMenuItem_Clicked() {
		
	}
	
	public void contentsMenuItem_Clicked() {
		
	}
	
	public void aboutMenuItem_Clicked() {
		AboutDialog a = new AboutDialog(this);
	}
	
	public void paintCompovnent(Graphics g) {
		//desktopPane.repaint();


	}
}
